package cn.kgc.controller;

import cn.kgc.pojo.Device;
import cn.kgc.service.DevicesService;
import cn.kgc.util.PageUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class DevicesController {
    @Resource
    private DevicesService devicesService;
    @RequestMapping(value = "/queryAllDevices")
    @ResponseBody
    public String queryAllDevices(String deviceName,int currentNo){
        int count=devicesService.queryCount(deviceName);
        PageUtil pageUtil=new PageUtil();
        pageUtil.setPageSize(3);
        pageUtil.setCount(count);
        pageUtil.setTotal(count);
        pageUtil.setCurrentNo(currentNo);
        List list= devicesService.queryAllDevice(deviceName,pageUtil.getCurrentNo(),pageUtil.getPageSize());
        list.add(pageUtil);
        return JSON.toJSONString(list);
    }


    @RequestMapping("/queryDevice/{deviceId}")
    public String queryDeviceById(@PathVariable int deviceId, Model model){
        Device device=devicesService.queryDeviceById(deviceId);
        model.addAttribute("device",device);
        return "devicedetail";
    }
}
